{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 正規表現"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 正規表現オブジェクト\n",
    "\n",
    "* 生成\n",
    "    * 両端を「/」で囲む\n",
    "    * 「%r」 とパーセント記法を用いる\n",
    "    * 生成されるとRegexpクラスのインスタンスになる"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "/Ruby/"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "/Ruby/\n",
    "#%r(Python)\n",
    "#Regexp.new /Perl/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## case式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\"Ruby!\"\n",
      "\"Ruby!\"\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\"Ruby!\""
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p case \"I love Ruby\"\n",
    "  when /Ruby/ then; p \"Ruby!\"\n",
    "  when /Java/ then; p \"Java!\"\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## ===演算子\n",
    "\n",
    "* 指定した文字列とマッチしているかを論理値で返す"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "true"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "/Ruby/ === \"I love Ruby\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## =~演算子\n",
    "\n",
    "* マッチした箇所を取得する\n",
    "    * マッチした場合、文字列のindexを返す\n",
    "    * マッチしなかった場合はnilを返す\n",
    " * =~演算子はStringクラスでも定義されている\n",
    "    * 左右を入れ替えても同じ結果になる"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "# 一番左を0番目として、7番目でヒット\n",
    "puts /Ruby/ =~ \"I love Ruby\"\n",
    "puts  \"I love Ruby\" =~ /Ruby/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## マッチした文字列を取り出す\n",
    "\n",
    "* `$&`\n",
    "    * マッチした文字列を取り出す\n",
    "* `$｀`\n",
    "    * マッチした文字列の直前の文字列を取り出す\n",
    "* $'\n",
    "    * マッチした文字列の直後の文字列を取り出す"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aa\n",
      "bb\n",
      "cc\n"
     ]
    }
   ],
   "source": [
    "/bb/ =~ \"aabbcc\"\n",
    "puts $`\n",
    "puts $&\n",
    "puts $'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 選択\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true\n",
      "true\n",
      "false\n",
      "false\n"
     ]
    }
   ],
   "source": [
    "reg = /^(aa|bb)c$/\n",
    "puts reg === \"aac\"\n",
    "puts reg === \"bbc\"\n",
    "puts reg === \"abc\"\n",
    "puts reg === \"aabbc\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 文字クラス"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true\n",
      "true\n",
      "false\n"
     ]
    }
   ],
   "source": [
    "reg = /a[bcd]e[fg]h/\n",
    "\n",
    "puts reg === \"abefh\"\n",
    "puts reg === \"adegh\"\n",
    "puts reg === \"aeh\""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Ruby 2.3.1",
   "language": "ruby",
   "name": "ruby"
  },
  "language_info": {
   "file_extension": ".rb",
   "mimetype": "application/x-ruby",
   "name": "ruby",
   "version": "2.3.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
